package com.web.netalbum.service.impl;

import com.web.netalbum.bean.User;
import com.web.netalbum.dao.UserDao;
import com.web.netalbum.dao.impl.UserDaoImpl;
import com.web.netalbum.service.UserService;
import com.web.netalbum.util.MD5Util;

public class UserServiceImpl implements UserService {
	private UserDao uDao=new UserDaoImpl();
	@Override
	public User login(String userName,String password) {
		password=MD5Util.md5Encode(password);
		User user=uDao.findUserByUserNameAndPassword(userName, password);
		return user;
	}

	@Override
	public boolean isExist(String userName) {
		User user=uDao.findUserByUserName(userName);
		if(user!=null){
			return true;
		}
		return false;
	}

	@Override
	public boolean register(User user) {
		user.setPassword(MD5Util.md5Encode(user.getPassword()));
		uDao.save(user);
		return true;
	}

	@Override
	public boolean active(String id, String code) {
		User user=uDao.findUserById(id);
		if(user!=null){
			if(user.isActived()){
				return true;
			}
			if(user.getCode().equals(code)){
				user.setActived(true);
				uDao.updateUser(user);
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean resetPasswordRequest(String id, String code) {
		User user=uDao.findUserById(id);
		if(user!=null){
			if(user.getCode().equals(code)){
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean updatePassword(String id, String password) {
		User user=uDao.findUserById(id);
		if(user!=null){
			user.setPassword(MD5Util.md5Encode(password));
			uDao.updateUser(user);
		}
		return false;
	}

	@Override
	public User existByEmail(String email) {
		User user=uDao.findUserByEmail(email);
		return user;
	}

	@Override
	public void updateUser(User user) {
		uDao.updateUser(user);
	}

	@Override
	public User findUser(String id) {
		User user=uDao.findUserById(id);
		return user;
	}

	@Override
	public User findUserByName(String userName) {
		
		return uDao.findUserByUserName(userName);
	}
	
	
}
